package com.isoft.src.main.java;

import com.isoft.src.main.java.model.Admin;
import com.isoft.src.main.java.model.Product;
import com.isoft.src.main.java.model.User;
import com.isoft.src.main.java.service.AdminService;
import com.isoft.src.main.java.service.ProductService;
import com.isoft.src.main.java.service.UserService;
import com.isoft.src.main.java.ui.AdminInterface;
import com.isoft.src.main.java.ui.UserInterface;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        ProductService productService = new ProductService();
        UserService userService = new UserService();
        AdminService adminService = new AdminService();

        // Sample data
        productService.addProduct(new Product("1", "Laptop", 999.99, 10));
        productService.addProduct(new Product("2", "Mouse", 19.99, 50));
        productService.addProduct(new Product("3", "Keyboard", 49.99, 30));

        // Create admin
        Admin admin = new Admin("admin", "adminpass");

        // Start admin interface
        AdminInterface adminInterface = new AdminInterface(adminService, productService);

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("1. 用户登录");
            System.out.println("2. 用户注册");
            System.out.println("3. 管理员登录");
            System.out.println("4. 退出");

            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    System.out.print("输入用户名: ");
                    String username = scanner.nextLine();
                    System.out.print("输入密码: ");
                    String password = scanner.nextLine();
                    if (userService.authenticate(username, password)) {
                        User user = new User(username, password);
                        UserInterface userInterface = new UserInterface(userService, productService, user);
                        userInterface.showMenu();
                    } else {
                        System.out.println("无效用户名或密码.");
                    }
                    break;
                case 2:
                    System.out.print("输入用户名: ");
                    String newUsername = scanner.nextLine();
                    System.out.print("输入密码: ");
                    String newPassword = scanner.nextLine();
                    User newUser = new User(newUsername, newPassword);
                    userService.registerUser(newUser);
                    break;
                case 3:
                    System.out.print("输入管理员用户名: ");
                    String adminUsername = scanner.nextLine();
                    System.out.print("输入管理员密码: ");
                    String adminPassword = scanner.nextLine();
                    if (adminService.authenticate(adminUsername, adminPassword)) {
                        adminInterface.showMenu();
                    } else {
                        System.out.println("无效密码.");
                    }
                    break;
                case 4:
                    System.out.println("正在退出...");
                    return;
                default:
                    System.out.println("无效选择");
            }
        }
    }
}
